using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace SunshineFarmWord
{
    public class AnimationEventTrigger : MonoBehaviour
    {
        public Action onAnimationEnterEvent;
        public Action onAnimationExitEvent;
        public Action onAnimationTransitionEvent;
        [SerializeField]
        public Animator animator;
        
        #region 动画方法
        public void StartAnimation(int animationHash)
        {
            animator?.SetBool(animationHash, true);
        }
        public void StopAnimation(int animationHash)
        {
            animator?.SetBool(animationHash, false);
        }
        #endregion
        
        #region 公共方法
        // 动画开始
        public void TriggerOnMovementStateAnimationEnterEvent()
        {
            if (IsInAnimationTransition())
            {
                return;
            }
            onAnimationEnterEvent?.Invoke();
        }
        // 动画结束
        public void TriggerOnMovementStateAnimationExitEvent()
        {
            if (IsInAnimationTransition())
            {
                return;
            }
            onAnimationExitEvent?.Invoke();
        }
        // 动画过渡
        public void TriggerOnMovementStateAnimationTransitionEvent()
        {
            if (IsInAnimationTransition())
            {
                return;
            }
            onAnimationTransitionEvent?.Invoke();
        }
        #endregion
        // 判断是否在动画过渡中
        private bool IsInAnimationTransition(int layerIndex = 0)
        {
            return animator?.IsInTransition(layerIndex) ?? false;
        }
    }
}
